function categoryTypeFormatter(value, row, index) {
    if (value == undefined || value == "") return "";
    else if (value == 1)
        return "竖状";
    else if (value == 2)
        return "横状";
    else return ""
}

function bannerTypeFormatter(value, row, index) {
    if (value == undefined || value == "") return "";
    else if (value == 0)
        return "无";
    else if (value == 1)
        return "短banner";
    else if (value == 2)
        return "大banner";
    else return ""
}

function listTypeFormatter(value, row, index) {
    if (value == undefined || value == "") return "";
    else if (value == 1)
        return "一行1列";
    else if (value == 2)
        return "一行2列";
    else return ""
}

function categoryLevelTypeFormatter(value, row, index) {
    if (value == undefined || value == "") return "";
    else if (value == 1)
        return "1级(无子品牌)";
    else if (value == 2)
        return "2级(含二级品牌)";
    else return ""
}

function tixianTypeFormatter(value, row, index) {
    if (value == undefined || value == "") return "";
    else if (value == 1)
        return "微信";
    else if (value == 2)
        return "支付宝";
    else return ""
}

function aboutTypeFormatter(value, row, index) {
    if (value == undefined || value == "") return "";
    else if (value == 0)
        return "基本配置模式";
    else if (value == 1)
        return "自编辑模式";
    else if (value == 2)
        return "官网地址";
    else return ""
}

function changeState(id, state) {
    $.ajax({
        type: 'POST',
        url: "changeState",
        data: {
            id: id,
            state: state
        },
        success: function () {
            $('#mytable').bootstrapTable('refresh', {url: '/admin/merchantconfig/list'});
        },
        dataType: "json"
    });
}

function deleteOne(id) {
    swal({
        title: '您确定要删除此行数据？',
        text: '你将无法恢复它！',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#ed5666',
        confirmButtonText: '确定删除！',
        cancelButtonText: '取消删除！',
        confirmButtonClass: 'btn btn-success',
        cancelButtonClass: 'btn btn-danger',
        buttonsStyling: false
    }).then(function () {

        $.ajax({
            type: 'POST',
            url: "deleteOne",
            data: {
                id: id,
            },
            success: function (status) {
                if (status == 1) {
                    swal({title: "删除成功", text: "", type: "success"})
                    $('#mytable').bootstrapTable('refresh', {url: '/admin/merchantconfig/list'});
                } else {
                    swal({title: "删除失败", text: "", type: "error"})
                }
            },
            dataType: "json"
        });

    }, function (dismiss) {
        // dismiss的值可以是'cancel', 'overlay',
        // 'close', 'timer'
        if (dismiss === 'cancel') {
            swal({title: '您已取消操作！', text: '', timer: 2000})
        }
    })
}

var editsw = [];
/**
 * 为模态框填充数据
 */
$('#modal-form').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget); // 触发事件的按钮
    var index = button.data('index');
    var modal = $(this);
    var data = $('#mytable').bootstrapTable("getData")[index]
    console.log(data)

    modal.find('#id').val(data.id)
    modal.find('#appid').val(data.appid)
    editsw["isShare"] == undefined ? editsw["isShare"] = new Switchery(document.querySelector('#isShare'), {color: "#1AB394"}) : ""
    if (data.isShare == 1) {
        setSwitchery(editsw["isShare"], true);
    } else {
        setSwitchery(editsw["isShare"], false);
    }
    editsw["isSearch"] == undefined ? editsw["isSearch"] = new Switchery(document.querySelector('#isSearch'), {color: "#1AB394"}) : ""
    if (data.isSearch == 1) {
        setSwitchery(editsw["isSearch"], true);
    } else {
        setSwitchery(editsw["isSearch"], false);
    }
    editsw["isMember"] == undefined ? editsw["isMember"] = new Switchery(document.querySelector('#isMember'), {color: "#1AB394"}) : ""
    if (data.isMember == 1) {
        setSwitchery(editsw["isMember"], true);
    } else {
        setSwitchery(editsw["isMember"], false);
    }
    editsw["isJifen"] == undefined ? editsw["isJifen"] = new Switchery(document.querySelector('#isJifen'), {color: "#1AB394"}) : ""
    if (data.isJifen == 1) {
        setSwitchery(editsw["isJifen"], true);
    } else {
        setSwitchery(editsw["isJifen"], false);
    }
    editsw["isCoupon"] == undefined ? editsw["isCoupon"] = new Switchery(document.querySelector('#isCoupon'), {color: "#1AB394"}) : ""
    if (data.isCoupon == 1) {
        setSwitchery(editsw["isCoupon"], true);
    } else {
        setSwitchery(editsw["isCoupon"], false);
    }
    editsw["isTmplmsg"] == undefined ? editsw["isTmplmsg"] = new Switchery(document.querySelector('#isTmplmsg'), {color: "#1AB394"}) : ""
    if (data.isTmplmsg == 1) {
        setSwitchery(editsw["isTmplmsg"], true);
    } else {
        setSwitchery(editsw["isTmplmsg"], false);
    }
    editsw["isMultishop"] == undefined ? editsw["isMultishop"] = new Switchery(document.querySelector('#isMultishop'), {color: "#1AB394"}) : ""
    if (data.isMultishop == 1) {
        setSwitchery(editsw["isMultishop"], true);
    } else {
        setSwitchery(editsw["isMultishop"], false);
    }
    editsw["isEcashCard"] == undefined ? editsw["isEcashCard"] = new Switchery(document.querySelector('#isEcashCard'), {color: "#1AB394"}) : ""
    if (data.isEcashCard == 1) {
        setSwitchery(editsw["isEcashCard"], true);
    } else {
        setSwitchery(editsw["isEcashCard"], false);
    }
    editsw["showCategory"] == undefined ? editsw["showCategory"] = new Switchery(document.querySelector('#showCategory'), {color: "#1AB394"}) : ""
    if (data.showCategory == 1) {
        setSwitchery(editsw["showCategory"], true);
    } else {
        setSwitchery(editsw["showCategory"], false);
    }
    modal.find('#categoryType').find("option[value='" + data['categoryType'] + "']").attr("selected", true);
    modal.find('#bannerType').find("option[value='" + data['bannerType'] + "']").attr("selected", true);
    modal.find('#listType').find("option[value='" + data['listType'] + "']").attr("selected", true);
    modal.find('#categoryLevelType').find("option[value='" + data['categoryLevelType'] + "']").attr("selected", true);
    modal.find('#jifenRate').val(data.jifenRate)
    modal.find('#maxJifen').val(data.maxJifen)
    modal.find('#memberDiscountRate').val(data.memberDiscountRate)
    modal.find('#minTixian').val(data.minTixian)
    modal.find('#tixianType').find("option[value='" + data['tixianType'] + "']").attr("selected", true);
    modal.find('#aboutType').find("option[value='" + data['aboutType'] + "']").attr("selected", true);
    modal.find('#website').val(data.website)
    $('#snaboutus2').code(data.aboutus);
    $('#aboutus2').val(data.aboutus);
    modal.find('#modifier').val(data.modifier)

})

//tableHeight函数
function tableHeight() {
    //可以根据自己页面情况进行调整
    return $(window).height() - 200;
}

$(function () {

    var sw = [];
    $(".js-switch").each(function (i, e) {
        sw[i] = new Switchery(e, {color: "#1AB394"})
    })
    $(window).resize(function () {
        $('#mytable').bootstrapTable('resetView', {
            height: tableHeight()
        })
    })
    //生成用户数据
    $('#mytable').bootstrapTable({
        method: 'get',
        url: "/admin/merchantconfig/list",
        // height:520,//高度调整
        // toolbar: '#toolbar',
        striped: true, //是否显示行间隔色
        dataField: "records",
        pagination: true,//是否分页
        queryParamsType: 'limit',
        queryParams: queryParams,
        sidePagination: 'server',
        pageNumber: 1, //初始化加载第一页，默认第一页
        pageSize: 50,//单页记录数
        pageList: [50, 100, 200, 500],//分页步进值
        showExport: true,//显示导出
        showRefresh: false,//刷新按钮
        showColumns: false,
        clikToSelect: true,//是否启用点击选中行
        // toolbarAlign:'right',
        // buttonsAlign:'right',//按钮对齐方式
        columns: [
            {
                title: '全选',
                field: 'select',
                checkbox: true,
                width: 25,
                align: 'center',
                valign: 'middle'
            },
            {
                title: 'ID',
                field: 'id',
                visible: false
            },
            {
                title: '应用名',
                field: 'appname'
            },
            {
                title: '支持分享',
                field: 'isShare',
                formatter: "sfFormatter"
            },
            {
                title: '支持搜索',
                field: 'isSearch',
                formatter: "sfFormatter"
            },
            {
                title: '支持会员',
                field: 'isMember',
                formatter: "sfFormatter"
            },
            {
                title: '支持积分',
                field: 'isJifen',
                formatter: "sfFormatter"
            },
            {
                title: '支持优惠券',
                field: 'isCoupon',
                formatter: "sfFormatter"
            },
            {
                title: '支持模板消息',
                field: 'isTmplmsg',
                formatter: "sfFormatter"
            },
            {
                title: '多门店',
                field: 'isMultishop',
                formatter: "sfFormatter"
            },
            {
                title: '余额支付',
                field: 'isEcashCard',
                formatter: "sfFormatter"
            },
            {
                title: '支持分类',
                field: 'showCategory',
                formatter: "sfFormatter"
            },
            {
                title: '目录类别',
                field: 'categoryType',
                formatter: "categoryTypeFormatter"
            },
            {
                title: 'banner类型',
                field: 'bannerType',
                formatter: "bannerTypeFormatter"
            },
            {
                title: '列表项类型',
                field: 'listType',
                formatter: "listTypeFormatter"
            },
            {
                title: '目录层级类别',
                field: 'categoryLevelType',
                formatter: "categoryLevelTypeFormatter"
            },
            {
                title: '多少积分兑换一元',
                field: 'jifenRate'
            },
            // {
            //     title: '最大消费积分',
            //     field: 'maxJifen'
            // },
            {
                title: '默认会员折扣',
                field: 'memberDiscountRate'
            },
            {
                title: '最低提现额度',
                field: 'minTixian'
            },
            {
                title: '提现方式',
                field: 'tixianType',
                formatter: "tixianTypeFormatter"
            },
            {
                title: '操作',
                field: 'Attribute',
                align: 'center',
                width: 170,
                formatter: actionFormatter
            }
        ],
        locale: 'zh-CN',//中文支持,
    })

    //请求服务数据时所传参数
    function queryParams(params) {
        console.log(params)
        return params;
    }

    /*
     * 验证错误提示信息高亮
     */
    $.validator.setDefaults({
        highlight: function (e) {
            $(e).closest(".form-group").removeClass("has-success").addClass("has-error")
        }, success: function (e) {
            e.closest(".form-group").removeClass("has-error").addClass("has-success")
        }, errorElement: "span", errorPlacement: function (e, r) {
            e.appendTo(r.is(":radio") || r.is(":checkbox") ? r.parent().parent().parent() : r.parent())
        }, errorClass: "help-block m-b-none", validClass: "help-block m-b-none"
    }), $("#aform").validate(), $("#mform").validate();
})

function checkAdd() {
    $('input:checkbox').each(function (index, item) {
        if ($(this).prop("checked")) {
            $(this).val(1);
            console.log($(this).attr("name") + "=" + $(this).val())
        } else {
            $(this).val(-1);
            console.log($(this).attr("name") + "=" + $(this).val())
        }
    });

    $('#aboutus').val($('#snaboutus').code())
    return true;
}

function checkModify() {
    $('input:checkbox').each(function (index, item) {
        if ($(this).prop("checked")) {
            $(this).val(1);
            console.log($(this).attr("id") + "=" + $(this).val())
        } else {
            $(this).val(-1);
            console.log($(this).attr("id") + "=" + $(this).val())
        }
    });

    $('#aboutus2').val($('#snaboutus2').code())
    return true;
}